$(function(){
	if(user == null || user == ''){
   		$("#loginOrRegist").show();
   	}else {
   		$("#top-index-usersName").text(user.usersName);
   		$("#usersName").show();
   	}
	
})

layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
	  var laydate = layui.laydate //日期
	  ,laypage = layui.laypage //分页
	  layer = layui.layer //弹层
	  ,table = layui.table //表格
	  ,upload = layui.upload //上传
	  ,element = layui.element; //元素操作
	 
	
	  var $ = layui.$;
	  $("#top-index-loginUrl").click(function(){
		  layer.open({
			  title: ' '
			  ,type: 2
			  ,area: ['38%','74%']
			  ,content: 'login.html'
			});
	  });
	  $("#top-index-registUrl").click(function(){
		  layer.open({
			  title: ' '
			  ,type: 2
			  ,area: ['38%','74%']
			  ,content: 'regist.html'
			});
	  });
	  
	  //查询购物车
	  var url = "../shoppingCar/findAll.action";
	  var data = null;
	  $.ajax({
		  url:url,
		  data:data,
		  async:false,
		  success:function(msg){
			  $("#sc-pro").html("");
			  for(var i=0;i<msg.length;i++){
				  $("#sc-pro").append('<div id="'+msg[i].shoppingcarId+'"><div class="sc-pro-list clearfix"><label class="checkbox"><input readonly="readonly" class="vam checked"> </label>'
									+'<div class="sc-pro-area"><div class="sc-pro-main clearfix"><a href="javascript:;" target="_blank" class="p-img"><img'
									+'	src="../'+msg[i].imgPath+'" alt="'+msg[i].proName+'"></a>'
									+'<div class="tips-1 p-stock-tips" style="display: none;"></div><ul><li><a href="jsvascript;;" target="_blank" title="'+msg[i].proName+'"'
									+'	class="p-name"> '+msg[i].proName+' '+msg[i].productCapacity+' '+msg[i].productColor+'</a></li><li><div class="p-price"><span>¥&nbsp;'+msg[i].productPrice+'</span>'
									+'</div></li><li><div class="p-stock"><div class="p-stock-area"><input type="number" class="p-stock-text" id="num_'+msg[i].proId+'" value='+msg[i].productNumber+'>'
									+'<p class="p-stock-btn"><a href="javascript:pro_number(\'jia\',\''+msg[i].proId+'\',\''+msg[i].shoppingcarId+'\');" class="">+</a> <a href="javascript:pro_number(\'jian\',\''+msg[i].proId+'\',\''+msg[i].shoppingcarId+'\');">−</a>'
									+'</p></div></div></li><li class="p-price-total">¥&nbsp;'+(parseInt(msg[i].productPrice)*(parseInt(msg[i].productNumber)))+'</li><li><a href="javascript:;" seed="cart-item-del"'
									+' class="p-del" shoppingCarId="'+msg[i].shoppingcarId+'">删除</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:;" seed="cart-item-del" class="p-edit" style="color:red" proId="'+msg[i].proId+'">立即结算</a></li>'
									+'</ul></div></div></div></div>');
			  }
		  }
	  });
	  $(".p-del").click(function(){
		  var obj = event.target;
		  var shoppingCarId = $(obj).attr("shoppingCarId");
		  layer.confirm('确认删除吗?', {title:'提示'}, function(index){
			  var data = {"shoppingcarId":shoppingCarId};
			  var url = "../shoppingCar/deletes.action";
			  $.ajax({
				  url:url,
				  data:data,
				  success:function(msg){
					  if(msg.state == "1"){
						  layer.msg("删除成功!",{icon:1,time:1000});
						  $("#"+shoppingCarId).remove();
					  }else {
						  layer.msg("删除失败!",{icon:1,time:1000});
					  }
				  }
			  });
			  
			  layer.close(index);
			});
	  });
	  
	  $(".p-edit").click(function(){
		  var obj = event.target;
		  var proId = $(obj).attr("proId");
		  var proQuantity = $("#num_"+proId).val();
		  location.href = "confirmOrder.html?proId="+proId+"&proQuantity="+proQuantity;
	  });
	  
	});
//商品数量
function pro_number(str,proId,shoppingcarId){
	var num = $("#num_"+proId).val();
	if(str == "jia"){
		num = parseInt(num) + 1;
	}else if(str == "jian"){
		num = parseInt(num) - 1;
		if(num <= 0){
			num = 1;
		}
	}
	var url = "../shoppingCar/update.action";
	var data = {"productNumber":num,"shoppingcarId":shoppingcarId};
	$.ajax({
		url:url,
		data:data,
		success:function(msg){
			location.reload();
		}
	});
}